package controller.commoncontroller;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.NumberFormatter;

import locsi.UserSettings;
import model.LogicType;
import model.editormodel.Input;
import model.editormodel.Output;
import view.UtilityConstants;
import view.UtilityLogicDesigns;
import view.commonview.TabbedPane;

public class SettingsListener implements ActionListener {

	private TabbedPane tabs;

	// -----------------------------------------------------------------------
	// Elemente zur Gestaltung des Einstellungsfensters
	private JDialog settingsDialog;
	private JTabbedPane pane = new JTabbedPane(JTabbedPane.TOP);
	private JPanel commonPanel = new JPanel();
	private JPanel designPanel = new JPanel(new BorderLayout());
	// -----------------------------------------------------------------------

	// selfdefinedpicture
	private JCheckBox paintSDPicture;

	// strg für selfdefinedpicture
	private JCheckBox needCTRLdownForPic;

	// tooltips an ein/ausgängen
	private JCheckBox showInOutTooltip;

	// kantenglättung
	private JCheckBox doAntiAlias;

	// farben ein/ausgänge
	private JLabel inputColorChooser;
	private JLabel outputColorChooser;

	// max anzahl bauteile wo noch circuitInfo berechnet wird
	private JLabel maxCircuitInfoLabel;
	private JFormattedTextField maxCircuitInfo;

	// max anzahl bauteile überschneidungsfrei
	private JLabel maxElemNoIntersectLabel;
	private JFormattedTextField maxElemNoIntersect;

	// radiobutton für validierungsmodus
	private JRadioButton validateAlways;
	private JRadioButton validateNever;
	private JRadioButton validateAsk;

	// offset für eingänge
	private JPanel offSetSetter;
	private JSpinner spinner;

	// look and feels
	private JPanel lafs;
	private JRadioButton standardButton;

	// defaults
	private JButton restoreDefaults;

	// akzeptieren
	private JButton accept;
	
	//das zuvor eingestellte design wird gespeichert um entscheiden zu koennen
	//ob neugestartet werden muss oder nicht
	private String designBefore;

	/**
	 * Konstruktor der den Listener initialisiert
	 * 
	 * @param tabs
	 */
	public SettingsListener(TabbedPane tabs) {
		this.tabs = tabs;
		initializeSettingsWindow();
	}

	/**
	 * Methode die das Einstellungsfenster initialisiert und alle noetigen
	 * Objekte erzeugt bzw. Listener addet
	 */
	private void initializeSettingsWindow() {
		settingsDialog = new JDialog(UtilityDialogs.getFrame());
		commonPanel = new JPanel();
		paintSDPicture = new JCheckBox(
				"Bild des selbstdefinierten Bauteils anzeigen",
				UserSettings.paintSDPicture);
		paintSDPicture.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {

				// wenn auf den knopf gedrueckt wird, wird zuerst die variable
				// in der Settingsklasse geaendert und dann entsprechende
				// aenderungen am einstellungsfenster sichtbar gemacht
				UserSettings.paintSDPicture = paintSDPicture.isSelected();
				if (UserSettings.paintSDPicture == false) {
					needCTRLdownForPic.setEnabled(false);
				} else {
					needCTRLdownForPic.setEnabled(true);
				}
			}
		});

		needCTRLdownForPic = new JCheckBox("STRG muss gedrückt sein",
				UserSettings.needControlDownForPicture);
		needCTRLdownForPic.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {

				// wenn auf den knopf gedrueckt wird, wird zuerst die variable
				// in der Settingsklasse geaendert und dann entsprechende
				// aenderungen am einstellungsfenster sichtbar gemacht
				UserSettings.needControlDownForPicture = needCTRLdownForPic
						.isSelected();
			}
		});
		if (!UserSettings.paintSDPicture) {
			needCTRLdownForPic.setEnabled(false);
		}

		showInOutTooltip = new JCheckBox(
				"<html><body>Tooltips für Ein/Ausgangsnamen <br>selbstdefinierter Bauteile anzeigen</body></html>",
				UserSettings.showInOutTooltip);
		showInOutTooltip.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {

				// wenn auf den knopf gedrueckt wird, wird zuerst die variable
				// in der Settingsklasse geaendert und dann entsprechende
				// aenderungen am einstellungsfenster sichtbar gemacht
				UserSettings.showInOutTooltip = showInOutTooltip.isSelected();
			}
		});

		validateAsk = new JRadioButton("auf Nachfrage validieren");
		validateAsk.setActionCommand("0");
		validateAlways = new JRadioButton("immer validieren (empfohlen)");
		validateAlways.setActionCommand("1");
		validateAlways
				.setToolTipText("<html><body><b>Achtung: </b> Kann bei Schaltungen > 10.000<br>Bauteilen zu langen Ladezeiten führen</body></html>");
		validateNever = new JRadioButton("nie validieren");
		validateNever
				.setToolTipText("<html><body><b>Achtung: </b> Kann bei ungültigen Schaltungen<br>zum Programmabsturz führen!</body></html>");
		validateNever.setActionCommand("-1");
		switch (UserSettings.askForValidateMode) {
		case 0:
			validateAsk.setSelected(true);
			break;
		case -1:
			validateNever.setSelected(true);
			break;
		case 1:
			validateAlways.setSelected(true);
			break;
		default:
			break;
		}
		ActionListener myChangeListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				UserSettings.askForValidateMode = Integer.parseInt(arg0
						.getActionCommand());
			}

		};
		validateAsk.addActionListener(myChangeListener);
		validateAlways.addActionListener(myChangeListener);
		validateNever.addActionListener(myChangeListener);
		ButtonGroup group = new ButtonGroup();
		group.add(validateAlways);
		group.add(validateAsk);
		group.add(validateNever);

		doAntiAlias = new JCheckBox("Kantenglättung aktiv",
				UserSettings.doAntiAlias);
		doAntiAlias
				.setToolTipText("<html><body>Linux-User sollten aus Performance-<br>gründen Kantenglättung deaktivieren</body></html>");
		doAntiAlias.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				UserSettings.doAntiAlias = doAntiAlias.isSelected();
				tabs.revalidate();
				tabs.repaint();
			}
		});

		inputColorChooser = new JLabel("Farbe Eingang");
		inputColorChooser.setBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED));
		inputColorChooser.setSize(85, 15);
		inputColorChooser.setPreferredSize(new Dimension(85, 15));
		inputColorChooser.setHorizontalAlignment(SwingConstants.CENTER);
		inputColorChooser.setOpaque(true);
		inputColorChooser.setBackground(UserSettings.inputColor);
		inputColorChooser.addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				JDialog dialog = new JDialog(settingsDialog);
				dialog.setTitle("Farbe auswählen...");
				final JColorChooser chooser = new JColorChooser(
						UserSettings.inputColor);
				chooser.getSelectionModel().addChangeListener(
						new ChangeListener() {

							@Override
							public void stateChanged(ChangeEvent e) {

								// wenn auf den knopf gedrueckt wird, wird
								// zuerst die variable
								// in der Settingsklasse geaendert und dann
								// entsprechende
								// aenderungen am einstellungsfenster sichtbar
								// gemacht
								UserSettings.inputColor = chooser.getColor();
								inputColorChooser.setBackground(chooser
										.getColor());
								offSetSetter.revalidate();
								offSetSetter.repaint();
								tabs.revalidate();
								tabs.repaint();
							}
						});
				dialog.add(chooser);
				dialog.pack();
				dialog.setModal(true);
				dialog.setLocationRelativeTo(settingsDialog);
				dialog.setVisible(true);
			}
		});

		outputColorChooser = new JLabel("Farbe Ausgang");
		outputColorChooser.setBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED));
		outputColorChooser.setSize(85, 15);
		outputColorChooser.setPreferredSize(new Dimension(85, 15));
		outputColorChooser.setHorizontalAlignment(SwingConstants.CENTER);
		outputColorChooser.setOpaque(true);
		outputColorChooser.setBackground(UserSettings.outputColor);
		outputColorChooser.addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				JDialog dialog = new JDialog(settingsDialog);
				dialog.setTitle("Farbe auswählen...");
				final JColorChooser chooser = new JColorChooser(
						UserSettings.outputColor);
				chooser.getSelectionModel().addChangeListener(
						new ChangeListener() {

							@Override
							public void stateChanged(ChangeEvent e) {

								// wenn auf den knopf gedrueckt wird, wird
								// zuerst die variable
								// in der Settingsklasse geaendert und dann
								// entsprechende
								// aenderungen am einstellungsfenster sichtbar
								// gemacht
								UserSettings.outputColor = chooser.getColor();
								outputColorChooser.setBackground(chooser
										.getColor());
								offSetSetter.revalidate();
								offSetSetter.repaint();
								tabs.revalidate();
								tabs.repaint();
							}
						});
				dialog.add(chooser);
				dialog.pack();
				dialog.setModal(true);
				dialog.setLocationRelativeTo(settingsDialog);
				dialog.setVisible(true);
			}
		});

		offSetSetter = new JPanel() {
			private static final long serialVersionUID = 1L;
			private float[] dash_array = { 2, 2 };

			@Override
			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				Graphics2D g2d = (Graphics2D) g;
				g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2d.setColor(Color.WHITE);
				int x = 17;
				int y = 9;
				g2d.fillRect(x - 20, y - 7, this.getWidth(),
						this.getHeight() - 5);

				g2d.setColor(Color.black);
				g2d.draw(UtilityLogicDesigns
						.getLogicDesign(LogicType.AND, x, y));
				g2d.setColor(UserSettings.inputColor);
				g2d.fillOval(
						x
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_A)[0],
						y
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_A)[1],
						UtilityConstants.IO_RADIUS, UtilityConstants.IO_RADIUS);
				g2d.setColor(Color.black);
				g2d.drawOval(
						x
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_A)[0],
						y
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_A)[1],
						UtilityConstants.IO_RADIUS, UtilityConstants.IO_RADIUS);
				g2d.setColor(UserSettings.inputColor);
				g2d.fillOval(
						x
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_B)[0],
						y
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_B)[1],
						UtilityConstants.IO_RADIUS, UtilityConstants.IO_RADIUS);
				g2d.setColor(Color.black);
				g2d.drawOval(
						x
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_B)[0],
						y
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_B)[1],
						UtilityConstants.IO_RADIUS, UtilityConstants.IO_RADIUS);

				g2d.setColor(UserSettings.outputColor);
				g2d.fillOval(
						x
								+ LogicType.AND
										.getStandardOutputCoordinate(Output.STD_O)[0],
						y
								+ LogicType.AND
										.getStandardOutputCoordinate(Output.STD_O)[1],
						UtilityConstants.IO_RADIUS, UtilityConstants.IO_RADIUS);
				g2d.setColor(Color.black);
				g2d.drawOval(
						x
								+ LogicType.AND
										.getStandardOutputCoordinate(Output.STD_O)[0],
						y
								+ LogicType.AND
										.getStandardOutputCoordinate(Output.STD_O)[1],
						UtilityConstants.IO_RADIUS, UtilityConstants.IO_RADIUS);

				Stroke strokeBefore = g2d.getStroke();
				g2d.setColor(Color.GRAY);
				g2d.setStroke(new BasicStroke(1f, BasicStroke.CAP_SQUARE,
						BasicStroke.JOIN_ROUND, 1f, dash_array, 0));
				g2d.drawOval(
						x
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_A)[0]
								- UserSettings.inputOutputOffset,
						y
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_A)[1]
								- UserSettings.inputOutputOffset,
						UtilityConstants.IO_RADIUS
								+ UserSettings.inputOutputOffset * 2,
						UtilityConstants.IO_RADIUS
								+ UserSettings.inputOutputOffset * 2);
				g2d.drawOval(
						x
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_B)[0]
								- UserSettings.inputOutputOffset,
						y
								+ LogicType.AND
										.getStandardInputCoordinate(Input.STD_B)[1]
								- UserSettings.inputOutputOffset,
						UtilityConstants.IO_RADIUS
								+ UserSettings.inputOutputOffset * 2,
						UtilityConstants.IO_RADIUS
								+ UserSettings.inputOutputOffset * 2);
				g2d.drawOval(
						x
								+ LogicType.AND
										.getStandardOutputCoordinate(Output.STD_O)[0]
								- UserSettings.inputOutputOffset,
						y
								+ LogicType.AND
										.getStandardOutputCoordinate(Output.STD_O)[1]
								- UserSettings.inputOutputOffset,
						UtilityConstants.IO_RADIUS
								+ UserSettings.inputOutputOffset * 2,
						UtilityConstants.IO_RADIUS
								+ UserSettings.inputOutputOffset * 2);

				g2d.setStroke(strokeBefore);
			}
		};
		offSetSetter.setBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED));
		offSetSetter
				.setToolTipText("<html><body>Hier kann der klickbare Bereich für Eingänge und<br>Ausgänge vergrößert bzw verkleinert werden.</body></html>");
		offSetSetter.setSize(65, 70);
		offSetSetter.setPreferredSize(new Dimension(65, 70));
		spinner = new JSpinner(new SpinnerNumberModel(
				UserSettings.inputOutputOffset, 0, 10, 1));
		((JSpinner.DefaultEditor) spinner.getEditor()).getTextField()
				.setEditable(false);
		spinner.setSize(40, 10);
		spinner.setPreferredSize(new Dimension(40, 10));
		spinner.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				System.out.println(maxCircuitInfo.getValue());
				UserSettings.inputOutputOffset = ((Number) spinner.getValue())
						.intValue();
				offSetSetter.revalidate();
				offSetSetter.repaint();
			}
		});

		lafs = new JPanel();
		lafs.setBorder(BorderFactory
				.createTitledBorder("GUI-Design (Neustart erforderlich)"));
		Box lafBox = Box.createVerticalBox();
		lafs.add(lafBox);
		final LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
		ButtonGroup bGroup = new ButtonGroup();
		for (final LookAndFeelInfo look : looks) {
			JRadioButton button = new JRadioButton(look.getName());

			if (UIManager.getCrossPlatformLookAndFeelClassName().equals(
					look.getClassName())) {
				button.setText(look.getName() + " (Standard)");
				standardButton = button;
			}

			button.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					UserSettings.actLookAndFeel = look.getClassName();
				}
			});
			lafBox.add(button);
			bGroup.add(button);
			if (look.getClassName().equals(UserSettings.actLookAndFeel)) {
				button.setSelected(true);
			}
		}

		restoreDefaults = new JButton("Standard wiederherstellen");
		restoreDefaults.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				restoreDefaults();
			}
		});

		accept = new JButton("OK");
		accept.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				UserSettings.maxElemCircuitInfo = (Integer) maxCircuitInfo
						.getValue();

				UserSettings.maxElemNoIntersect = (Integer) maxElemNoIntersect
						.getValue();

				// einstellungen werden gespeichert und das einstellungsfenster
				// geschlossen
				UserSettings.writeSettingsFile();
				settingsDialog.dispose();
				
				if (!UserSettings.actLookAndFeel.equals(designBefore) && JOptionPane.YES_OPTION == JOptionPane
						.showConfirmDialog(
								null,
								"<html><body>Das GUI-Design wurde geändert. Dass die Änderungen sichtbar<br>werden, muss neugestartet werden. Jetzt neustarten?</body></html>",
								"Programmneustart...?",
								JOptionPane.YES_NO_OPTION)) {
					try {
						Runtime.getRuntime().exec("java -jar *.jar");
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					System.exit(0);
				}
			}
		});

		maxCircuitInfoLabel = new JLabel("Schaltungsinformationen für max. ");
		NumberFormatter nformat = new NumberFormatter();
		nformat.setAllowsInvalid(false);
		nformat.setValueClass(Integer.class);
		nformat.setMaximum(new Integer(9999999));
		nformat.setMinimum(new Integer(0));
		maxCircuitInfo = new JFormattedTextField(nformat);
		maxCircuitInfo.setValue(UserSettings.maxElemCircuitInfo);
		maxCircuitInfo.setColumns(5);
		maxCircuitInfo
				.setToolTipText("<html><body><b>Achtung:</b> Bei mehr als 500 Bauteilen<br> kann es zu langen Rechenzeiten kommen!</body></html>");

		maxElemNoIntersectLabel = new JLabel(
				"Überschneidungsfreiheit für max. ");
		maxElemNoIntersect = new JFormattedTextField(nformat);
		maxElemNoIntersect.setValue(UserSettings.maxElemNoIntersect);
		maxElemNoIntersect.setColumns(5);
		maxElemNoIntersect
				.setToolTipText("<html><body><b>Achtung:</b> Bei mehr als 10000 Bauteilen<br> kann es zu erhöhten Rechenzeiten kommen!</body></html>");

		// Boxen für 1. Reiter
		Box commonBox = Box.createVerticalBox();

		// erster reiter bekommt elemente
		commonBox.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		paintSDPicture.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
		commonBox.add(paintSDPicture);
		needCTRLdownForPic.setBorder(BorderFactory.createEmptyBorder(1, 30, 10,
				1));
		commonBox.add(needCTRLdownForPic);
		showInOutTooltip
				.setBorder(BorderFactory.createEmptyBorder(1, 1, 10, 1));
		commonBox.add(showInOutTooltip);

		// validatemodeBox
		Box centerItBox = Box.createHorizontalBox();
		centerItBox.add(Box.createHorizontalGlue());
		Box askValidateModeBox = Box.createVerticalBox();
		centerItBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		centerItBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		askValidateModeBox.setBorder(BorderFactory
				.createTitledBorder("Zu ladende Schaltungen..."));
		askValidateModeBox.add(validateAsk);
		askValidateModeBox.add(validateAlways);
		askValidateModeBox.add(validateNever);
		centerItBox.add(askValidateModeBox);
		centerItBox.add(Box.createHorizontalGlue());
		centerItBox.setBorder(BorderFactory.createEmptyBorder(1, 1, 10, 1));
		commonBox.add(centerItBox);

		commonBox.add(new JSeparator());

		// kantenglättung
		doAntiAlias.setBorder(BorderFactory.createEmptyBorder(6, 1, 10, 1));
		commonBox.add(doAntiAlias);

		// maxcircuitInfoBox
		Box maxCircuitInfoBox = Box.createHorizontalBox();
		maxCircuitInfoBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		maxCircuitInfoBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		maxCircuitInfoBox.add(maxCircuitInfoLabel);
		maxCircuitInfoBox.add(maxCircuitInfo);
		maxCircuitInfoBox.setBorder(BorderFactory
				.createEmptyBorder(1, 1, 10, 1));
		commonBox.add(maxCircuitInfoBox);

		// maxElemNoIntersectBox
		Box maxElemNoIntersectBox = Box.createHorizontalBox();
		maxElemNoIntersectBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		maxElemNoIntersectBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		maxElemNoIntersectBox.add(maxElemNoIntersectLabel);
		maxElemNoIntersectBox.add(maxElemNoIntersect);
		maxElemNoIntersectBox.setBorder(BorderFactory.createEmptyBorder(1, 1,
				10, 1));
		commonBox.add(maxElemNoIntersectBox);

		commonBox.add(new JSeparator());

		// boxelement auf dem 1. reiter auf dem die colorchooser horizontal
		// angeordnet sind
		Box hBox = Box.createVerticalBox();
		hBox.setBorder(BorderFactory.createEmptyBorder(6, 1, 10, 1));
		hBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		hBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		hBox.add(Box.createVerticalGlue());
		inputColorChooser.setMaximumSize(new Dimension(inputColorChooser
				.getPreferredSize().width + 20, inputColorChooser
				.getPreferredSize().height + 20));
		inputColorChooser.setPreferredSize(new Dimension(inputColorChooser
				.getPreferredSize().width + 20, inputColorChooser
				.getPreferredSize().height + 20));
		hBox.add(inputColorChooser);
		outputColorChooser.setMaximumSize(new Dimension(outputColorChooser
				.getPreferredSize().width + 20, outputColorChooser
				.getPreferredSize().height + 20));
		outputColorChooser.setPreferredSize(new Dimension(outputColorChooser
				.getPreferredSize().width + 20, outputColorChooser
				.getPreferredSize().height + 20));
		hBox.add(outputColorChooser);
		// hBox.add(Box.createVerticalGlue());
		// commonBox.add(hBox);

		// offsetBox
		Box hOffColorBox = Box.createHorizontalBox();
		hOffColorBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		hOffColorBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		Box hOffSetBox = Box.createHorizontalBox();
		hOffSetBox.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createEmptyBorder(1, 1, 6, 1),
				BorderFactory.createTitledBorder("Ein-/Ausgangs - Offsets")));
		hOffSetBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		hOffSetBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		hOffSetBox.add(Box.createHorizontalGlue());
		hOffSetBox.add(offSetSetter);
		hOffSetBox.add(Box.createHorizontalStrut(5));
		Box spinnerBox = Box.createVerticalBox();
		spinnerBox.add(Box.createVerticalGlue());
		spinnerBox.add(spinner);
		spinnerBox.add(Box.createVerticalGlue());
		hOffSetBox.add(spinnerBox);
		hOffSetBox.add(Box.createHorizontalGlue());
		hOffColorBox.add(hOffSetBox);
		hOffColorBox.add(hBox);
		commonBox.add(hOffColorBox);

		// look and feels-box
		lafs.setAlignmentX(Component.LEFT_ALIGNMENT);
		lafs.setAlignmentY(Component.LEFT_ALIGNMENT);
		Box designBox = Box.createVerticalBox();
		designBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		designBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		designBox.add(lafs);
		designBox.setSize(new Dimension(commonBox.getPreferredSize().width, designBox.getPreferredSize().height));
		designBox.setPreferredSize((new Dimension(commonBox.getPreferredSize().width, designBox.getPreferredSize().height)));
		
		designPanel.add(designBox);

		// button zum zuruecksetzen wird geaddet
		Box hBox2 = Box.createHorizontalBox();
		hBox2.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY),
				BorderFactory.createEmptyBorder(10, 1, 10, 1)));
		hBox2.setAlignmentX(Component.LEFT_ALIGNMENT);
		hBox2.setAlignmentY(Component.LEFT_ALIGNMENT);
		hBox2.add(Box.createHorizontalGlue());
		hBox2.add(restoreDefaults);
		hBox2.add(Box.createHorizontalGlue());
//		commonBox.add(hBox2);

		// ok button (zum speichern) wird geaddet
		Box hBox3 = Box.createHorizontalBox();
		hBox3.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY),
				BorderFactory.createEmptyBorder(10, 0, 0, 0)));
		hBox3.setAlignmentX(Component.LEFT_ALIGNMENT);
		hBox3.setAlignmentY(Component.LEFT_ALIGNMENT);
		hBox3.add(Box.createHorizontalGlue());
		hBox3.add(accept);
		hBox3.add(Box.createHorizontalGlue());
//		commonBox.add(hBox3);

		commonPanel.add(commonBox, BorderLayout.CENTER);
		pane.addTab("Allgemein", commonPanel);
		pane.addTab("GUI-Design", designPanel);

		settingsDialog.add(pane, BorderLayout.CENTER);
		Box buttonBox = Box.createVerticalBox();
		buttonBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		buttonBox.setAlignmentY(Component.LEFT_ALIGNMENT);
		buttonBox.add(hBox2);
		buttonBox.add(hBox3);
		buttonBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
		settingsDialog.add(buttonBox, BorderLayout.SOUTH);
		settingsDialog.setTitle("LOCSI - Einstellungen");
		settingsDialog.setResizable(false);
		settingsDialog.setModal(true);
		settingsDialog.pack();
		settingsDialog.setLocationRelativeTo(tabs);
	}

	/**
	 * Beim Klick auf den Menuepunkt Einstellungen, wird der Einstellungs-
	 * dialog angezeigt
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (settingsDialog.getIconImages().isEmpty()) {
			settingsDialog.setIconImage(UtilityDialogs.getFrame()
					.getIconImage());
		}
		designBefore = UserSettings.actLookAndFeel;
		settingsDialog.setVisible(true);
	}

	/**
	 * Methode die die Standardeinstellungen wieder herstellt
	 */
	private void restoreDefaults() {
		UserSettings.restoreDefaultSettings();
		paintSDPicture.setSelected(UserSettings.paintSDPicture);
		needCTRLdownForPic.setSelected(UserSettings.needControlDownForPicture);
		showInOutTooltip.setSelected(UserSettings.showInOutTooltip);
		inputColorChooser.setBackground(UserSettings.inputColor);
		outputColorChooser.setBackground(UserSettings.outputColor);
		maxCircuitInfo.setText("" + UserSettings.maxElemCircuitInfo);
		maxElemNoIntersect.setText("" + UserSettings.maxElemNoIntersect);
		doAntiAlias.setSelected(UserSettings.doAntiAlias);
		spinner.setValue(new Integer(UserSettings.inputOutputOffset));
		validateAlways.setSelected(true);
		standardButton.setSelected(true);

		offSetSetter.revalidate();
		offSetSetter.repaint();
		tabs.revalidate();
		tabs.repaint();
	}
}
